var addressManager = new Vue({

    el: "#addressManager",

    data: {
        dialogFormVisible: false,
        //地址表
        addressTable: [],
        //单个询价单信息
        form: {
            address:'',
            affiliatedRegional:'',
        },
        //当前页
        currentPage:'1',
        //页面总条数
        pagesize:'10',


    },
    //提交时间方法
    methods: {
        //格式化时间
        dateFormat:function(row,column) {
            var date = row[column.property];
            if (date == undefined) {

            }
            return moment(date).format("YYYY-MM-DD");
        },
        //页面总条数
        handleSizeChange:function(size){
            var _this=this;
            _this.pagesize = size;
        },
        //当前页
        handleCurrentChange:function(currentPage){
            var _this=this;
            _this.currentPage = currentPage;
        },
        updateData: function (rowIndex,row) {
            var _this=this;
            debugger
            $.ajaxSetup({
                async: false
            });
            $.post("/home/addressManager/updateAddress", {"data": JSON.stringify(row)}, function (data) {
                if(data.code==200){
                    _this.$notify({
                        message: "更新成功",
                        type: 'success'
                    });
                }else{
                    _this.$notify({
                        message: "更新失败",
                        type: 'error'
                    });
                }
            });
            $("#wrap-left").load("http://" + window.location.host + "/home/addressManagerList.html", function () {

            });
        },
        checked:function (rowIndex,row) {
            var _this=this;
            debugger
            $.ajaxSetup({
                async: false
            });
            $.post("/home/addressManager/updateAddressFlag", {"addressData": JSON.stringify(row)}, function (data) {
                if(data.code==200){
                    if(data.code==200){
                        _this.$notify({
                            message: "操作成功",
                            type: 'success'
                        });
                    }else{
                        _this.$notify({
                            message: "操作失败",
                            type: 'error'
                        });
                    }
                }
            });
            $("#wrap-left").load("http://" + window.location.host + "/home/addressManagerList.html", function () {

            });
        },
        deleteData:function (Index,row) {
            var _this=this;
            debugger
            $.ajaxSetup({
                async: false
            });
            if(row[(_this.currentPage-1)*_this.pagesize+Index].ischeck){
                _this.$notify({
                    message: "对不起，该地址是您的默认地址，如果需要删除，请先设置其他地址为默认地址！",
                    type: 'error'
                });
                return;
            }
            $.post("/home/addressManager/deleteAddress", {"data": JSON.stringify(row[(_this.currentPage-1)*_this.pagesize+Index])}, function (data) {
                    if(data.code==200){
                        _this.$notify({
                            message: "删除成功",
                            type: 'success'
                        });
                    }else{
                        _this.$notify({
                            message: "删除失败",
                            type: 'error'
                        });
                    }
            });
            row.splice(row[(_this.currentPage-1)*_this.pagesize+Index],1);
            $("#wrap-left").load("http://" + window.location.host + "/home/addressManagerList.html", function () {

            });
        },
        tableRowClassName:function(row, rowIndex) {
            var _this = this;
            if (rowIndex === 0) {
                return 'warning-row';
            }else if(rowIndex === 1) {
                return 'success-row';
            }else if (rowIndex==2) {
                return 'warning-row';
            }else if (rowIndex%2!=0) {
                return 'success-row';
            }else{
                return 'warning-row';
            }
            return '';
        },
        //新增页面
        addressAdd: function () {
            debugger
            $("#address2").show();
            $("#address1").hide();
        },
        certen: function () {
            $("#address2").hide();
            $("#address1").show();
        },
        saveaddress: function () {
            var _this=this;
            debugger
            if(_this.form.address!=null&&_this.form.address!=''){
            _this.form.affiliatedRegional =  $("#province1").val() + "-" + $("#city1").val() + "-" + $("#district1").val();
            var address=_this.form.affiliatedRegional+_this.form.address;
            $.post("/home/addressManager/insertAddress", {"data": address}, function (data) {
                if(data.code==200){
                    _this.$notify({
                        message: "操作成功",
                        type: 'success'
                    });
                }else{
                    _this.$notify({
                        message: "操作失败",
                        type: 'error'
                    });
                }
            });
                $("#wrap-left").load("http://" + window.location.host + "/home/addressManagerList.html", function () {

                });
            }else{
                _this.$notify({
                    message: "对不起，详细地址信息必须填写",
                    type: 'error'
                });
                return;
            }
        },
    },
    //页面加载后的初始化方法
    created: function () {
        var _this = this;
        //查询跟自己 有关系的地址
        $.post("/home/addressManager/selectByUserAndType", "", function (data) {
            debugger
            _this.addressTable = data.data.addressManagers;
        });
        $("#address2").hide();
        $("#address1").show();
    }
});
function tableRowClassName(row, rowIndex) {
    var _this = this;
    if (rowIndex === 1) {
        return 'warning-row';
    } else if (rowIndex === 3) {
        return 'success-row';
    }
    return '';
}